<?php

/**
 * 后台友情链接模块 Model
 * ============================================================================
 * TBlog TBlog博客系统　BY Tmac PHP MVC framework
 * $Author: zwt007 $  <zwttmac@qq.com>
 * $Id: link.class.php 31 2011-07-11 15:25:51Z zwt007 $
 * http://www.t-mac.org；
 */
class link extends Model
{

    private $link_table;

    /**
     * 初始化变量　定义私有变量
     */
    public function _init()
    {
        //连接数据库
        $this->connect();
        $this->link_table = '#@__links';
    }

    /**
     * 保存
     * @param string $menusMain
     * return Boole
     */
    public function addlink($info, $mode = 'INSERT', $where = '')
    {
        $rs = $this->db->autoExecute($this->link_table, $info, $mode, $where);
        return $rs;
    }

    /**
     * 获取一个友情链接信息
     * @param int $class_id 栏目id
     * return array
     */
    public function getlinkInfo($id)
    {
        $rs = $this->db->getRow("SELECT * FROM $this->link_table WHERE link_id = $id LIMIT 0,1");
        return $rs;
    }

    /**
     * 获取所有资讯
     * return article_class,pages
     */
    public function getlinkList($url = null)
    {
        $count = $this->db->getOne("SELECT COUNT(*) FROM $this->link_table");
        if ($url == '')
            $url = PHP_SELF . '?m=link&page=';
        $pages = $this->P('Pages');
        $pages->setTotal($count);
        $pages->setUrl($url);
        $pages->setPrepage(10);
        $limit = $pages->getSqlLimit();
        $rs = $this->db->getAll("SELECT link_id, link_name, link_url, link_type_radio, link_state_radio, link_time FROM $this->link_table ORDER BY link_id DESC LIMIT $limit");

        //友情状态数组
        $link_state_radio_ary = UtilityConfig::ad_state_radio_ary();
        //友情类型数组
        $link_type_radio_ary = UtilityConfig::link_type_radio_ary();
        //遍历通过class_id取class_name
        if (is_array($rs)) {
            foreach ($rs AS $k => $v) {
                $rs[$k]['state_name'] = $link_state_radio_ary[$v['link_state_radio']];
                $rs[$k]['type_name'] = $link_type_radio_ary[$v['link_type_radio']];
                $rs[$k]['time'] = date('Y-m-d H:i:s', $v['link_time']);
            }
        }

        //把文章的当前page写到cookies里
        HttpResponse::setCookie('link_page', $pages->getNowPage());

        if ($count == 0) {
            $ErrorMsg = "暂无资讯文章!   <a href='" . PHP_SELF . "?m=link.add' class='link_a'>点我来添加新友情链接</a>";
        } else {
            $ErrorMsg = '';
        }

        $this->assign('rs', $rs);
        $this->assign('pageCurrent', $pages->getNowPage());
        $this->assign('page', $pages->show());
        $this->assign('ErrorMsg', $ErrorMsg);
        return $rs;
    }

    /**
     * del
     * @param int $id
     */
    public function dellink($id)
    {
        $rs = $this->db->execute("DELETE FROM $this->link_table WHERE link_id in ($id)");
        return $rs;
    }

}